function sum(a: number, b: number): number {
    return a + b;
}

// sum(1) // 报错
// 少了或者增加了参数，都是不被允许的
sum(1, 2)


let x = function (a: number, b: number) {
    console.log("你好")
}


let y = (x: number, z: number): number => {
    return x * z
}

// 接口定义函数的形状
interface SearchFn {
    // 可选参数，必须接在参数的后面
    (keyword: string, type?: string): string;
}

let mySearch: SearchFn = function (keyword: string, type: string) {
    return "123"
}

mySearch('123')

// 默认值
function printName(firstName:string='薄', lastName:string='荷'): string {
    return firstName + lastName
}


function calc(type:string,...items:number[]):number {
    if (type === 'add') {
        return items.reduce((pre,item)=>pre + item)
    } else {
        return items.reduce((pre,item)=>pre * item)
    }
}

calc('add', 1,2,3)
calc('asd',1,2,3)

// function demo(x:number| string):number|string {
//     return x
// }

function demo(x:number):number;
function demo(x:string):string;
function demo(x:number|string):number|string {
    if (typeof x === 'number') {
        return  x + 6;
    } else if (typeof x === 'string') {
        return x + '天气真耶！'
    }
}
